<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<!-- template designed by Marco Von Ballmoos -->
			<title>Using PEL in applications</title>
			<link rel="stylesheet" href="../media/stylesheet.css" />
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>
		</head>
		<body>
			<div class="page-body">			
	<table class="tutorial-nav-box">
	<tr>
		<td style="width: 30%">
							<a href="../PEL/tutorial_PEL.pkg.html"><img src="../media/images/previous_button.png" alt="Previous"></a>
					</td>
		<td style="text-align: center">
							<a href="../PEL/tutorial_PEL.pkg.html"><img src="../media/images/up_button.png" alt="Up"></a>
					</td>
		<td style="text-align: right; width: 30%">
							<a href="../PEL/tutorial_faq.pkg.html"><img src="../media/images/next_button.png" alt="Next"></a>
					</td>
	</tr>
	<tr>
		<td style="width: 30%">
							<span class="detail">PHP Exif Library</span>
					</td>
		<td style="text-align: center">
							<span class="detail">PHP Exif Library</span>
					</td>
		<td style="text-align: right; width: 30%">
							<span class="detail">Frequently Asked Questions (FAQ)</span>
					</td>
	</tr>
</table>
	
<div><a name=""></a><div class="ref-title-box">Using PEL in applications<h1 class="ref-title">Using PEL in applications</h1>
    <h2 class="ref-purpose">Learn how to load, edit, and save images</h2></div>
  <div class="ref-synopsis"><div class="author-blurb"><p><span class="author">Martin
          Geisler</span>
        <a href="mailto:mgeisler@users.sourceforge.net">mgeisler@users.sourceforge.net</a></p></div></div>

  <h1 class="title">Table of Contents</h1>
<ul class="toc">
	
			
					<li><a href="../PEL/tutorial_using.pkg.html#jpeg">Loading a JPEG image</a></li>
					
					<li><a href="../PEL/tutorial_using.pkg.html#tiff">Obtaining the TIFF data</a></li>
					
					<li><a href="../PEL/tutorial_using.pkg.html#reading">Reading Values</a></li>
					
					<li><a href="../PEL/tutorial_using.pkg.html#writing">Writing Values</a></li>
					
					<li><a href="../PEL/tutorial_using.pkg.html#saving">Saving an Image</a></li>
					</ul>


  <span><a name="jpeg"></a><h2 class="title">Loading a JPEG image</h2><p>The typical use for PEL is to read and write data from JPEG
    images.  Such an image is represented in PEL using an object of
    the PelJpeg class.  With the filename of a
    JPEG image stored in the variable $filename,
    then it is a simple matter of creating a <a href="../PEL/PelJpeg.html">PelJpeg</a>
    object:</p>

    <div class="src-code"><ol><li><div class="src-line"><span class="src-var">$jpeg&nbsp;</span>=&nbsp;<span class="src-key">new&nbsp;</span><span class="src-id"><a href="../PEL/PelJpeg.html">PelJpeg</a></span><span class="src-sym">(</span><span class="src-var">$filename</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div>

    <p>If this succeeded without any exceptions being thrown, one
    can proceed to find the Exif data itself. The Exif data is
    retrieved using the <a href="../PEL/PelJpeg.html#methodgetExif">PelJpeg::getExif()</a> method:</p>

    <div class="src-code"><ol><li><div class="src-line"><span class="src-var">$exif&nbsp;</span>=&nbsp;<span class="src-var">$jpeg</span><span class="src-sym">-&gt;</span><span class="src-id">getExif</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div>

    <p>The section stored in $exif is now a
    <a href="../PEL/PelExif.html">PelExif</a> object.</p>

    <span class="warning"><p>If the JPEG image does not contain Exif information, then
      the $exif variable will be
      <code>null</code>.</p></span></span>


  <span><a name="tiff"></a><h2 class="title">Obtaining the TIFF data</h2><p>The Exif data is not stored directly in this object, instead
    it is stored in a <a href="../PEL/PelTiff.html">PelTiff</a> object, which can be retrieved
    using the <a href="../PEL/PelExif.html#methodgetTiff">getTiff()</a> method:</p>

    <div class="src-code"><ol><li><div class="src-line"><span class="src-var">$tiff&nbsp;</span>=&nbsp;<span class="src-var">$exif</span><span class="src-sym">-&gt;</span><span class="src-id">getTiff</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div>

    <p>This peculiar step is necessary because what one normally
    thinks of as Exif data is really just an extension of the TIFF
    standard.  PEL models this by having the <a href="../PEL/PelExif.html">PelExif</a> object
    contain a <a href="../PEL/PelTiff.html">PelTiff</a> object.</p>

    <p>TIFF data is organized as a chain of Image File Directories
    (IFDs), each represented by a <a href="../PEL/PelIfd.html">PelIfd</a> object.  Each IFD has
    a number of entries (<a href="../PEL/PelEntry.html">PelEntry</a> objects) which one can get
    hold of using the <a href="../PEL/PelIfd.html#methodgetEntry">getEntry()</a>
    method.</p>

    <p>The first IFD, number zero, will normally contain the
    <a href="../PEL/PelTag.html#constIMAGE_DESCRIPTION">IMAGE_DESCRIPTION</a> tag. The
    following code will initialize $ifd0 with the
    first IFD, and $desc with the
    description:</p>

    <div class="src-code"><ol><li><div class="src-line"><span class="src-var">$ifd0&nbsp;</span>=&nbsp;<span class="src-var">$tiff</span><span class="src-sym">-&gt;</span><a href="../PEL/PelTiff.html#methodgetIfd">getIfd</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-var">$desc&nbsp;</span>=&nbsp;<span class="src-var">$ifd0</span><span class="src-sym">-&gt;</span><span class="src-id">getEntry</span><span class="src-sym">(</span><span class="src-id"><a href="../PEL/PelTag.html">PelTag</a></span><span class="src-sym">::</span><span class="src-id">IMAGE_DESCRIPTION</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div>

    <p>Now $desc will contain a
    <a href="../PEL/PelEntryAscii.html">PelEntryAscii</a> object holding the description.  Each entry
    is represented using an object of a class descendent of
    <a href="../PEL/PelEntry.html">PelEntry</a>.  There are classes for numbers such as
    <a href="../PEL/PelEntryShort.html">PelEntryShort</a> for small numbers and <a href="../PEL/PelEntryLong.html">PelEntryLong</a>
    for big numbers, and more specialized classes, such as
    <a href="../PEL/PelEntryVersion.html">PelEntryVersion</a> for version numbers, <a href="../PEL/PelEntryTime.html">PelEntryTime</a>
    for date and time, and so on.</p></span>

  <span><a name="reading"></a><h2 class="title">Reading Values</h2><p>The value of any entry can be retrieved by calling the
    <a href="../PEL/PelEntry.html#methodgetValue">getValue()</a> method on the object.
    Doing this on $desc will return a string, while
    doing it on a <a href="../PEL/PelEntryShort.html">PelEntryShort</a> will normally return an
    integer (see <a href="../PEL/PelEntryNumber.html#methodgetValue">the documentation</a>
    for the full story).  So to echo the description one simply
    does:</p>
    
    <div class="src-code"><ol><li><div class="src-line">echo&nbsp;<span class="src-str">'The&nbsp;description:&nbsp;'&nbsp;</span>.&nbsp;<span class="src-var">$desc</span><span class="src-sym">-&gt;</span><span class="src-id">getValue</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div></span>

  <span><a name="writing"></a><h2 class="title">Writing Values</h2><p>Writing new values (changing values) to an entry is just as
    easy as reading values, one just uses the
    <a href="../PEL/PelEntry.html#methodsetValue">setValue()</a> method on the entry in
    question.</p>

    <p>Continuing on our example from before where
    $desc contains a <a href="../PEL/PelEntryAscii.html">PelEntryAscii</a> object
    with the description for the image, one changes the description
    with:</p>

    <div class="src-code"><ol><li><div class="src-line"><span class="src-var">$desc</span><span class="src-sym">-&gt;</span><span class="src-id">setValue</span><span class="src-sym">(</span><span class="src-str">'The&nbsp;new&nbsp;description.'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol></div>

    <p>The object is now updated and is ready to be turned back
    into bytes, so that the image can be saved with the new, updated
    description.</p></span>

  <span><a name="saving"></a><h2 class="title">Saving an Image</h2><p>After having changed an image, one would probably want to
    save it to keep the changes.</p>

    <p><a href="../PEL/PelJpeg.html">PelJpeg</a> objects (and <a href="../PEL/PelTiff.html">PelTiff</a> objects) can be
    turned back into bytes with the <a href="../PEL/PelJpeg.html#methodgetBytes">    getBytes</a> method.  This will turn the object and all the objects
    within it into bytes, which can then be saved in a file to produce
    a JPEG image.</p>

    <p>With the image loaded into $jpeg it is a
    simple matter to write the new JPEG file:</p>

    <div class="src-code"><ol><li><div class="src-line"><a href="http://www.php.net/file_put_contents">file_put_contents</a><span class="src-sym">(</span><span class="src-str">'new-'&nbsp;</span>.&nbsp;<span class="src-var">$filename</span><span class="src-sym">,&nbsp;</span><span class="src-var">$jpeg</span><span class="src-sym">-&gt;</span><a href="../PEL/PelTiff.html#methodgetBytes">getBytes</a><span class="src-sym">(</span><span class="src-sym">))</span><span class="src-sym">;</span></div></li>
</ol></div></span></div>

	<table class="tutorial-nav-box">
	<tr>
		<td style="width: 30%">
							<a href="../PEL/tutorial_PEL.pkg.html"><img src="../media/images/previous_button.png" alt="Previous"></a>
					</td>
		<td style="text-align: center">
							<a href="../PEL/tutorial_PEL.pkg.html"><img src="../media/images/up_button.png" alt="Up"></a>
					</td>
		<td style="text-align: right; width: 30%">
							<a href="../PEL/tutorial_faq.pkg.html"><img src="../media/images/next_button.png" alt="Next"></a>
					</td>
	</tr>
	<tr>
		<td style="width: 30%">
							<span class="detail">PHP Exif Library</span>
					</td>
		<td style="text-align: center">
							<span class="detail">PHP Exif Library</span>
					</td>
		<td style="text-align: right; width: 30%">
							<span class="detail">Frequently Asked Questions (FAQ)</span>
					</td>
	</tr>
</table>
	
	<p class="notes" id="credit">
		Documentation generated on Tue, 19 Dec 2006 01:08:11 +0100 by <a href="http://www.phpdoc.org" target="_blank">phpDocumentor 1.3.0</a>
                <a target="_top" href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=108380&amp;type=1" width="88" height="31" border="0" align="right" alt="SourceForge.net Logo" /></a>
	</p>
	</div></body>
</html>